package com.woniu.his.cms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.his.bms.client.BillRecordClient;
import com.woniu.his.bms.client.result.BillRecordResult;
import com.woniu.his.cms.common.CmsResultCode;
import com.woniu.his.cms.common.enums.CmsStatusEnums;
import com.woniu.his.cms.infra.model.NonDrug;
import com.woniu.his.cms.infra.model.NonDrugItemRecord;
import com.woniu.his.cms.infra.mapper.NonDrugItemRecordMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.his.cms.service.NonDrugItemRecordService;
import com.woniu.his.cms.service.NonDrugService;
import com.woniu.his.cms.service.dto.NonDrugItemDto;
import com.woniu.his.cms.service.param.AddNonDrugItemRecordServiceParam;
import com.woniu.his.cms.service.param.UpdateNonDrugItemServiceRecord;
import com.woniu.his.dms.client.CaseHistoryClient;
import com.woniu.his.dms.client.result.CaseHistoryResult;
import com.woniu.his.exception.HisException;
import com.woniu.his.result.Result;
import com.woniu.his.sms.client.SmsStaffClient;
import com.woniu.his.sms.client.result.LoginStaffResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 非药品项目记录表 服务实现类
 * </p>
 *
 * @author dx
 * @since 2022-11-28
 */
@Service
public class NonDrugItemRecordServiceImpl extends ServiceImpl<NonDrugItemRecordMapper, NonDrugItemRecord> implements NonDrugItemRecordService {

    @Resource
    private SmsStaffClient smsStaffClient;

    @Resource
    private NonDrugService nonDrugService;

    @Resource
    private CaseHistoryClient caseHistoryClient;

    @Resource
    private BillRecordClient billRecordClient;

    @Override
    public void addNonDrugItemRecord(AddNonDrugItemRecordServiceParam serviceParam) {

        //分别查询执行人员，创建人员是否存在
        //1.创建人员
        Result<LoginStaffResult> createStaff = smsStaffClient.findLoginStaffById(serviceParam.getCreateStaffId());
        if (createStaff.getData()==null) throw new HisException(CmsResultCode.USE_EXCEPTION);
        //2.执行人员
        Result<LoginStaffResult> excuteStaff = smsStaffClient.findLoginStaffById(serviceParam.getExcuteStaffId());
        if (excuteStaff.getData()==null) throw new HisException(CmsResultCode.USE_EXCEPTION);
        //查询非药品是否存在,并计算价格
        NonDrug nonDrug = nonDrugService.getById(serviceParam.getNoDrugId());
        if (nonDrug==null) throw new HisException(CmsResultCode.NON_DRUG_NOT_EXIST);
        //查询病历是否存在
        Result<CaseHistoryResult> caseHistoryResultResult = caseHistoryClient.checkCaseHistoryExist(serviceParam.getCaseHistoryId());
        if (!caseHistoryResultResult.isSuccess()) throw new HisException(CmsResultCode.USE_EXCEPTION);
        //生成订单  ####
        BigDecimal amount=new BigDecimal(0);
        amount=nonDrug.getPrice();
        Result<BillRecordResult> billRecord = billRecordClient.createBillRecord(serviceParam.getType(),amount,serviceParam.getCaseHistoryId());
        if (!billRecord.isSuccess()) throw new HisException(CmsResultCode.USE_EXCEPTION);

        NonDrugItemRecord record= BeanUtil.copyProperties(serviceParam,NonDrugItemRecord.class);
        record.setNonDrugItemStatus(CmsStatusEnums.NOT_PAY.getCode());
        record.setCreateTime(new Date());
        record.setBillId(billRecord.getData().getId());
        record.setExcuteDeptId(excuteStaff.getData().getDeptId());
        record.setExcuteTime(new Date());
        record.setAmount(nonDrug.getPrice());
        save(record);

    }

    @Override
    public void deleteNonDrugItemRecord(Long id) {

        NonDrugItemRecord one = getById(id);
        if (one == null) throw new HisException(CmsResultCode.NON_DRUG_NOT_EXITS);
        if (one.getNonDrugItemStatus().equals(CmsStatusEnums.DELETE.getCode())) throw new HisException(CmsResultCode.NON_DRUG_DELETED_WRONG);
        one.setNonDrugItemStatus(CmsStatusEnums.DELETE.getCode());
        updateById(one);
    }

    @Override
    public void updateNonDrugItemRecord(UpdateNonDrugItemServiceRecord record) {

        NonDrugItemRecord one = getById(record.getId());
        if (one == null) throw new HisException(CmsResultCode.NON_DRUG_NOT_EXITS);
        if (one.getNonDrugItemStatus().equals(CmsStatusEnums.DELETE.getCode())) throw new HisException(CmsResultCode.NON_DRUG_DELETED_WRONG);
        one.setCheckResult(record.getCheckResult());
        one.setResultImgUrlList(record.getResultImgUrlList());
        one.setEndDatetime(new Date());
        updateById(one);

    }

    @Override
    public NonDrugItemDto queryNonDrugItemByBillId(Long billId) {
        NonDrugItemRecord itemRecord = getOne(new QueryWrapper<NonDrugItemRecord>().eq("bill_id", billId));
        if(itemRecord==null) throw new HisException(CmsResultCode.NON_DRUG_ITEM_NOT_EXITS);
        NonDrugItemDto drugItemDto = BeanUtil.copyProperties(itemRecord, NonDrugItemDto.class);
        NonDrug nonDrug = nonDrugService.getOne(new QueryWrapper<NonDrug>().eq("id", itemRecord.getNoDrugId()));
        drugItemDto.setNonDrugName(nonDrug.getNonDrugName());
        return drugItemDto;
    }

    @Override
    public Boolean paySuccess(Long billId) {
        NonDrugItemRecord itemRecord = getOne(new QueryWrapper<NonDrugItemRecord>().eq("bill_id", billId));
        itemRecord.setNonDrugItemStatus("NOT_REGISTERED");
        updateById(itemRecord);
        return true;
    }
}
